package cn.anc.aonicardv.net;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.KeyEvent;

import cn.anc.aonicardv.R;
import cn.anc.aonicardv.bean.ResponseResultBean;
import cn.anc.aonicardv.util.DialogUtils;
import rx.Subscriber;

/**
 * Created by yangdai on 2017/11/14.
 */

public abstract class ProgressSubscriber extends Subscriber<ResponseResultBean> implements DialogInterface.OnKeyListener {

    private ProgressDialog progressDialog;

    public ProgressSubscriber(Context context) {
        progressDialog =new DialogUtils().getProgressDialog(context);
        progressDialog.setMessage(context.getString(R.string.wait_please));
        progressDialog.setOnKeyListener(this);
    }

    @Override
    public void onStart() {
        if (progressDialog != null)
            progressDialog.show();
    }

    @Override
    public void onCompleted() {
        if (progressDialog != null)
            progressDialog.dismiss();
    }

    @Override
    public void onError(Throwable throwable) {
        if (progressDialog != null)
            progressDialog.dismiss();
    }

    @Override
    public void onNext(ResponseResultBean o) {
        if (progressDialog != null)
            progressDialog.dismiss();

    }

    public void onCancel() {

    }

    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            unsubscribe();
            progressDialog.dismiss();
            onCancel();
        }
        return true;
    }
}
